home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / total / textcounter / counter.pl next >
Encoding:
Perl Script  |  1996-03-15  |  5.0 KB  |  170 lines

  1. #!/usr/local/bin/perl
  2. ##############################################################################
  3. # WWWCount Text Version (Requires Server Side Includes) Version 1.0
  4. # Created by Matt Wright    mattw@worldwidemart.com
  5. # Created on: 3/14/96        Last Modified on: 3/14/96
  6. # Installation Instructions found in README file.
  7. ##############################################################################
  8. # Define Variables
  9.  
  10. # Data Dir is the directory on your server that you wish to store the 
  11. # count files in.  Each page that has the counter on it will have it's own 
  12. # file.  
  13.  
  14. $data_dir = "/path/to/data/";
  15.  
  16. # Valid-Referer allows you to limit servers that can use your counter 
  17. # program.  It is suggested that you put in your base domain (similar to 
  18. # myself using worldwidemart.com) and the IP address of your server (mine 
  19. # being 206.31.72.203)
  20.  
  21. @valid_referer = ('worldwidemart.com','206.31.72.203');
  22.  
  23. # Valid-URI allows you to set up the counter to only work under specific 
  24. # directories on your server.  Include any of these directories as they 
  25. # appear in a URI, into this array.  More information on URI's available in 
  26. # README.
  27.  
  28. @valid_uri = ('/');
  29.  
  30. # Invalid-URI allows the owner of this script to set up the counter so 
  31. # that certain portions of the web server that may be included in Valid-URI 
  32. # cannot use the program.  Leave this commented out if you wish not to 
  33. # block out certain parts.
  34.  
  35. # @invalid_uri = ('/');
  36.  
  37. ##############################################################################
  38. # Set Options
  39.  
  40. # Show Link allows you to add a link around the counter to point to 
  41. # either instructions explaining to users how to set this up on the system 
  42. # (useful if a system administrator wants to allow anyone to set things up 
  43. # themselves).  Setting it to 0 will make no link, otherwise put the URL
  44. # you want linked to the count here.
  45.  
  46. $show_link = "http://www.worldwidemart.com/scripts/";
  47.  
  48. # When Auto-Create is enabled, users will be able to auto-create the 
  49. # count on their home pages by simply imbedding the Server Side Includes 
  50. # call.  Setting auto_create to 1 enables it, 0 will disable it. Only 
  51. # users in @referers and @valid_uri will be allowed to auto create.
  52.  
  53. $auto_create = "1";
  54.  
  55. # Show Date will show the date of when the count began if you set this 
  56. # option to 1.  It will appear in yor document as [Count] hits since [Date].
  57. # Set this to 0 and it will simply return the [Count].
  58.  
  59. $show_date = "1";
  60.  
  61. ##############################################################################
  62.  
  63. # Print Content Type Header For Browser
  64. print "Content-type: text/html\n\n";
  65.  
  66. $count_page = "$ENV{'DOCUMENT_URI'}";
  67. if ($count_page =~ /\/$/) {
  68.    chop($count_page);
  69. }
  70. $count_page =~ s/\//_/g;
  71.  
  72. # Check Referer and URI
  73. &check_referer_and_uri;
  74.  
  75. if (-e "$data_dir$count_page") {
  76.    open(COUNT,"$data_dir$count_page");
  77.    $line = <COUNT>;
  78.    chop($line) if $line =~ /\n$/;
  79.    close(COUNT);
  80.    ($date,$count) = split(/\|\|/,$line);
  81. }
  82. elsif ($auto_create == 1) {
  83.    &create; 
  84. }
  85. else {
  86.    &error('page_not_found');
  87. }
  88.  
  89. $count++;
  90. if ($show_date == 1) {
  91.    if ($show_link =~ /http:\/\//) {
  92.       print "<a href=\"$show_link\">$count</a> hits since $date";
  93.    }
  94.    else {
  95.       print "$count hits since $date";
  96.    }
  97. }
  98. else {
  99.    if ($show_link =~ /http:\/\//) {
  100.       print "<a href=\"$show_link\">$count</a>";
  101.    }
  102.    else {
  103.       print "$count";
  104.    }
  105. }
  106.  
  107. open(COUNT,">$data_dir$count_page") || &error('could_not_increment');
  108. print COUNT "$date\|\|$count";
  109. close(COUNT);
  110.  
  111. sub check_referer_and_uri {
  112.    $referer_check = "0"; $uri_check = "0";
  113.    foreach $referer (@valid_referer) {
  114.       if (!($ENV{'HTTP_REFERER'})) {
  115.          $referer_check = "1";
  116.       }
  117.       elsif ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
  118.      $referer_check = "1";
  119.          last;
  120.       }
  121.    }
  122.  
  123.    foreach $uri (@valid_uri) {
  124.       if ($ENV{'DOCUMENT_URI'} =~ /$uri/) {
  125.          $uri_check = "1";
  126.          last;
  127.       }
  128.    }
  129.  
  130.    foreach $uri (@invalid_uri) {
  131.       if ($ENV{'DOCUMENT_URI'} =~ /$uri/) {
  132.          $uri_check = "0";
  133.      last;
  134.       }
  135.    }
  136.  
  137.    if ($referer_check == 0 || $uri_check == 0) {
  138.       &error('bad_referer_or_uri');
  139.    }
  140. }
  141.  
  142. sub create {
  143.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  144.    @months = ("January","February","March","April","May","June","July",
  145.           "August","September","October","November","December");
  146.    $date = "@months[$mon] $mday, 19$year";
  147.    $count = "0";
  148.    open(COUNT,">$data_dir$count_page") || &error('count_not_created');
  149.    print COUNT "$date\|\|$count";
  150.    close(COUNT);
  151. }
  152.  
  153. sub error {
  154.    $error = shift(@_);
  155.  
  156.    if ($error eq 'page_not_found') {
  157.       print "[WWWCount Fatal Error: This Page Not Found\; Auto-Create Option Disabled]";
  158.    }
  159.    elsif ($error eq 'bad_referer_or_uri') {
  160.       print "[WWWCount Fatal Error: This Page Not In Valid Referer or URI]";
  161.    }
  162.    elsif ($error eq 'count_not_created') {
  163.       print "[WWWCount Fatal Error: Could Not Write to File $datadir$count_page]";
  164.    }
  165.    elsif ($error eq 'could_not_increment') {
  166.       print "[WWWCount Fatal Error: Could Not Increment Counter]";
  167.    }
  168.    exit;
  169. }
  170.